package cn.kgc.dao;

import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class BaseDao<T> {
    public Connection getConnection(){
        Connection con = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/school_management","root","root");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return con;
    }

    public void closeAll(ResultSet rs,PreparedStatement ps,Statement st,Connection con){
        try {
            if(rs!=null){
                rs.close();
            }
            if(ps!=null){
                ps.close();
            }
            if(st!=null){
                st.close();
            }
            if(con!=null){
                con.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    public List<T> query(Class<T> clazz, String sql,Object ... params){
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<T> list = new ArrayList<T>();
        try {
            con = this.getConnection();
            ps = con.prepareStatement(sql);
            if(params!=null){
                for (int i = 0; i < params.length; i++) {
                    ps.setObject(i+1,params[i]);
                }
            }
            rs = ps.executeQuery();
            ResultSetMetaData metaData = rs.getMetaData();//获取元数据;
            int columnCount = metaData.getColumnCount();//获取列的个数
            while(rs.next()){
                T t = clazz.newInstance();
                for (int i = 1; i <= columnCount; i++) {
                    String columnname = metaData.getColumnLabel(i);
                    Object data = rs.getObject(columnname);
                    try {
                        Field field = clazz.getDeclaredField(columnname);
                        field.setAccessible(true);
                        field.set(t,data);
                    } catch (NoSuchFieldException e) {
                        System.out.println("实体类中没有"+columnname+"属性");
                    }
                }
                list.add(t);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }finally {
            this.closeAll(rs,ps,null,con);
        }
        return list;
    }

    public int execute(String sql,Object ... params){
        Connection con = null;
        PreparedStatement ps = null;
        int Count = 0;
        try {
            con = this.getConnection();
            ps = con.prepareStatement(sql);
            if(params != null){
                for (int i = 0; i < params.length; i++) {
                    ps.setObject(i+1,params[i]);
                }
            }
            Count = ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            this.closeAll(null,ps,null,con);
        }
        return Count;
    }
}
